package mo.offer_special;

import mo.common.TreeNode;

public class L053 {

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode successor = null;
        if (p.right != null) {
            successor = p.right;
            while (successor.left != null) {
                successor = successor.left;
            }
            return successor;
        }
        TreeNode node = root;
        while (node != null) {
            if (node.val > p.val) {
                node = node.left;
                successor = node;
            } else {
                node = node.right;
            }
        }
        return successor;
    }



}
